package icu.yougan.trade.service.Impl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import icu.yougan.trade.entity.User;

import icu.yougan.trade.mapper.UserMapper;
import icu.yougan.trade.service.UserService;
import icu.yougan.trade.vo.PageVo;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import java.io.OutputStream;
import java.util.List;

/**
 * @Author:油柑
 * @changeByLyh
 * @Date:2024/5/15 2:42
 * @Version:v1.0.0
 * @Description:TODO
 **/
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getUser(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User getUserByAccountNum(String accountNumber) {
        return userMapper.selectByAccountNum(accountNumber);
    }

    @Override
    public User userLogin(String accountNumber, String userPassword) {
        return userMapper.userLogin(accountNumber, userPassword);
    }

    @Override
    public boolean userSignIn(User user) {
        return userMapper.insert(user) == 1;
    }

    @Override
    public boolean updateUserInfo(User user) {
        return userMapper.updateByPrimaryKeySelective(user) == 1;
    }

    @Override
    public boolean updatePassword(String newPassword, String oldPassword, Long id) {
        return userMapper.updatePassword(newPassword, oldPassword, id) == 1;
    }

    @Override
    public PageVo<User> getUserByStatus(int status, int page, int nums) {
        List<User> list;
        int count = 0;
        if (status == 0) {
            count = userMapper.countNormalUser();
            list = userMapper.getNormalUser((page - 1) * nums, nums);
        } else {
            count = userMapper.countBanUser();
            list = userMapper.getBanUser((page - 1) * nums, nums);
        }
        return new PageVo<>(list, count);
    }

    @Override
    public void exportUsers(String startDate, String endDate, OutputStream outputStream) {
        // 查询数据
        List<User> users = userMapper.selectByDateRange(startDate, endDate);

        // 写入Excel
        try (ExcelWriter excelWriter = EasyExcel.write(outputStream).build()) {
            WriteSheet sheet = EasyExcel.writerSheet("用户数据")
                    .head(User.class)
                    .build();
            excelWriter.write(users, sheet);
        }
    }

    @Override
    public User getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }

    @Override
    public boolean updatePasswordDirectly(Long id, String newPassword) {
        if(id==null||newPassword==null)
        {
            return false;
        }
        return userMapper.updatePasswordDirectly(id,newPassword);
    }
}